package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnbgService;
import com.augurit.gzcgwsp.jzfqwczzxn.web.form.YwYnJzfqwczzXnForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IXnBgService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 消纳 变更
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class XnBgServiceImpl implements IXnBgService{
	Logger logger = (Logger) LoggerFactory.getLogger(XnBgServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwYnJzfqwczzXnbgService ywYnJzfqwczzXnbgService;
	
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String xnBgfilepath = localResource.getString("xnBgfilepath");

	// 附件存放路径
	private final String xnBgfujianpath = localResource.getString("xnBgfujianpath");

	// 解析成功反馈xml存放路径
	private final String xnBgfeedbackPath = localResource.getString("xnBgfeedbackPath");

	// 解析异常xml存放路径
	private final String xnBgexceptionPath = localResource.getString("xnBgexceptionPath");

	// 解析后，xml备份
	private final String xnBgbackPath = localResource.getString("xnBgbackPath");

	private Map<String, String> xnBgKeyMap;// xml内容的key

	private Map<String, String> xnBgKeyMapHead;// xml头部的key

	private Map<String, Long> xnBgfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initxnBgMap() {
		// 初始化数据部分对应的map
		xnBgKeyMap = new HashMap<String, String>();
		xnBgKeyMap.put("approvalNo", "approvalNo");
		xnBgKeyMap.put("businessCode", "businessCode");
		xnBgKeyMap.put("YSL_NO", "YSL_NO");
		xnBgKeyMap.put("sendTime", "sendTime");
		
		xnBgKeyMap.put("GCDZ", "GCDZ");
		xnBgKeyMap.put("SQDW", "SQDW");
		xnBgKeyMap.put("GCMC", "GCMC");
		xnBgKeyMap.put("JSDW_DZ", "JSDW_DZ");
		xnBgKeyMap.put("JSDW_LXR", "JSDW_LXR");
		xnBgKeyMap.put("JSDW_LXR_LXDH", "JSDW_LXR_LXDH");
		xnBgKeyMap.put("SGDW_GZ", "SGDW_GZ");
		xnBgKeyMap.put("SGDW_XMJL", "SGDW_XMJL");
		xnBgKeyMap.put("SGDW_XMJL_LXDH", "SGDW_XMJL_LXDH");
		xnBgKeyMap.put("SGDW_LXR", "SGDW_LXR");
		xnBgKeyMap.put("SGDW_LXR_LXDH", "SGDW_LXR_LXDH");
		xnBgKeyMap.put("SBJZFQWCZL", "SBJZFQWCZL");
		

		// 初始化头部对应的map
		xnBgKeyMapHead = new HashMap<String, String>();
		xnBgKeyMapHead.put("sn", "sn");
		xnBgKeyMapHead.put("sender", "sender");
		xnBgKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		xnBgfujianMap = new HashMap<String, Long>();
		xnBgfujianMap.put("FJ_01", 656l);
		xnBgfujianMap.put("FJ_02", 657l);
		xnBgfujianMap.put("FJ_03", 658l);
		xnBgfujianMap.put("FJ_04", 659l);
		xnBgfujianMap.put("FJ_05", 660l);
		xnBgfujianMap.put("FJ_06", 661l);
		xnBgfujianMap.put("FJ_07", 662l);
		xnBgfujianMap.put("FJ_08", 663l);
		xnBgfujianMap.put("FJ_09", 664l);
		xnBgfujianMap.put("FJ_10", 665l);
		xnBgfujianMap.put("FJ_11", 666l);
		xnBgfujianMap.put("FJ_12", 667l);
		xnBgfujianMap.put("FJ_13", 668l);
		xnBgfujianMap.put("FJ_14", 669l);
		xnBgfujianMap.put("FJ_15", 670l);
		xnBgfujianMap.put("FJ_16", 671l);
		xnBgfujianMap.put("FJ_17", 672l);
		xnBgfujianMap.put("FJ_18", 673l);
		xnBgfujianMap.put("FJ_19", 674l);
		xnBgfujianMap.put("FJ_20", 675l);
		xnBgfujianMap.put("FJ_21", 676l);
		xnBgfujianMap.put("FJ_22", 677l);
		xnBgfujianMap.put("FJ_23", 678l);
		xnBgfujianMap.put("FJ_24", 679l);
		xnBgfujianMap.put("FJ_25", 680l);
		xnBgfujianMap.put("FJ_26", 681l);
		xnBgfujianMap.put("FJ_27", 682l);
		xnBgfujianMap.put("FJ_28", 683l);
		xnBgfujianMap.put("FJ_29", 684l);
		xnBgfujianMap.put("FJ_30", 685l);
		xnBgfujianMap.put("FJ_31", 686l);
		xnBgfujianMap.put("FJ_32", 687l);
		xnBgfujianMap.put("FJ_33", 688l);
		xnBgfujianMap.put("FJ_34", 689l);
		xnBgfujianMap.put("FJ_35", 690l);
		xnBgfujianMap.put("FJ_36", 691l);
		xnBgfujianMap.put("FJ_37", 692l);
		xnBgfujianMap.put("FJ_38", 693l);
		xnBgfujianMap.put("FJ_39", 694l);
		xnBgfujianMap.put("FJ_40", 695l);
		xnBgfujianMap.put("FJ_41", 696l);
		xnBgfujianMap.put("FJ_42", 697l);
		xnBgfujianMap.put("FJ_43", 698l);
		xnBgfujianMap.put("FJ_44", 699l);
		xnBgfujianMap.put("FJ_45", 700l);
		xnBgfujianMap.put("FJ_46", 701l);
		xnBgfujianMap.put("FJ_47", 702l);
		xnBgfujianMap.put("FJ_48", 703l);
		xnBgfujianMap.put("FJ_49", 704l);
		xnBgfujianMap.put("FJ_50", 705l);
		xnBgfujianMap.put("FJ_51", 706l);
		xnBgfujianMap.put("FJ_52", 707l);
		xnBgfujianMap.put("FJ_53", 708l);
		xnBgfujianMap.put("FJ_54", 709l);
		xnBgfujianMap.put("FJ_55", 710l);
		xnBgfujianMap.put("FJ_56", 711l);
		xnBgfujianMap.put("FJ_57", 712l);
		xnBgfujianMap.put("FJ_58", 713l);
		xnBgfujianMap.put("FJ_59", 714l);
		xnBgfujianMap.put("FJ_60", 715l);
		xnBgfujianMap.put("FJ_61", 716l);
		xnBgfujianMap.put("FJ_62", 717l);
		xnBgfujianMap.put("FJ_63", 718l);
		xnBgfujianMap.put("FJ_64", 719l);
		xnBgfujianMap.put("FJ_65", 720l);
		xnBgfujianMap.put("FJ_66", 721l);
		xnBgfujianMap.put("FJ_67", 722l);
		xnBgfujianMap.put("FJ_68", 723l);
		xnBgfujianMap.put("FJ_69", 724l);
		xnBgfujianMap.put("FJ_70", 725l);
		xnBgfujianMap.put("FJ_71", 726l);
		xnBgfujianMap.put("FJ_72", 727l);
	}

	public void readXml(String projectPath) {
		if (xnBgKeyMap == null)
			this.initxnBgMap();
		File file = new File(xnBgfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(xnBgKeyMap, filePath,xnBgfeedbackPath, xnBgexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(xnBgKeyMapHead,files[i], xnBgfeedbackPath, xnBgexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczzXnForm form = new YwYnJzfqwczzXnForm();
						for (String mapKey : xnBgKeyMap.keySet()) {
							
							form.setYslNo(parseXMLService.getMapValue(xnBgKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(xnBgKeyMap.get("SQDW")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(xnBgKeyMap.get("SQDW")),"jzfqwczzxn_bg");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(xnBgKeyMap.get("ADDRESS")));
							form.setGcdz(parseXMLService.getMapValue(xnBgKeyMap.get("GCDZ")));
							form.setGcmc(parseXMLService.getMapValue(xnBgKeyMap.get("GCMC")));
							form.setJsdwDz(parseXMLService.getMapValue(xnBgKeyMap.get("JSDW_DZ")));
							form.setJsdwLxr(parseXMLService.getMapValue(xnBgKeyMap.get("JSDW_LXR")));
							form.setJsdwLxrLxdh(parseXMLService.getMapValue(xnBgKeyMap.get("JSDW_LXR_LXDH")));
							form.setSgdwGz(parseXMLService.getMapValue(xnBgKeyMap.get("SGDW_GZ")));
							form.setSgdwXmjl(parseXMLService.getMapValue(xnBgKeyMap.get("SGDW_XMJL")));
							form.setSgdwXmjlLxdh(parseXMLService.getMapValue(xnBgKeyMap.get("SGDW_XMJL_LXDH")));
							form.setSgdwLxr(parseXMLService.getMapValue(xnBgKeyMap.get("SGDW_LXR")));
							form.setSgdwLxrLxdh(parseXMLService.getMapValue(xnBgKeyMap.get("SGDW_LXR_LXDH")));
							form.setSbjzfqwczl(parseXMLService.getMapValue(xnBgKeyMap.get("SBJZFQWCZL")));

							form.setBranch("jzfqwczzxn_bg");
							form.setApprovalNo(parseXMLService.getMapValue(xnBgKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(xnBgKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(xnBgKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywYnJzfqwczzXnbgService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									xnBgKeyMapHead, xnBgfeedbackPath, xnBgexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, xnBgfujianpath, xnBgfeedbackPath, xnBgexceptionPath, xnBgKeyMapHead,
								files, xnBgfujianMap, i, form.getQyid(), form.getId(), "jzfqwczzxn_bg");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								xnBgKeyMapHead, xnBgfeedbackPath, xnBgexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, xnBgbackPath + fileName,fileName);
				}
			}
		}
	}
}
